package cn.xspace.app.filemg.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.imgload.core.DisplayImageOptions;
import com.xspace.android.xafcommon.imgload.core.assist.ImageScaleType;
import com.xspace.android.xafcommon.imgload.core.display.FadeInBitmapDisplayer;
import com.xspace.android.xafcommon.util.BaseRProxy;
import com.xspace.android.xafcommon.util.ImageUtils;
import com.xspace.android.xafcommon.util.StringUtils;

import java.util.List;

import cn.xspace.app.filemg.bean.FileInfo;
import cn.xspace.app.filemg.bean.ImgPackInfo;
import cn.xspace.app.filemg.util.CallBack;
import cn.xspace.app.filemg.util.FileMg;

public class FileImgAdapter extends BaseExpandableListAdapter implements
		CallBack {
	Context context;
	List<ImgPackInfo> parentlist;
	private DisplayImageOptions options;
	private CallBack callBack;

	public FileImgAdapter(Context context, List<ImgPackInfo> parentlist,
			CallBack callBack) {
		super();
		this.context = context;
		this.parentlist = parentlist;
		this.callBack = callBack;
		options = new DisplayImageOptions.Builder().showImageForEmptyUri(BaseRProxy.picturndefault1).showImageOnFail(BaseRProxy.picturndefault2).resetViewBeforeLoading().cacheInMemory()
				.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).displayer(new FadeInBitmapDisplayer(300)).build();
	   
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		return parentlist.get(arg0).grdList;
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return arg1;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(BaseRProxy.file_app_child_item, null);
			viewHolder = new ViewHolder(convertView);
		
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final FileInfo appInfo=parentlist.get(groupPosition).getImgList().get(childPosition);
		viewHolder.title.setText(appInfo.getName());
		viewHolder.appSize.setText(FileMg.FormetFileSize(appInfo.getSize()));
		viewHolder.appTime.setText(appInfo.getTime());
		if (!TextUtils.isEmpty(appInfo.getPath())) {
			ImageUtils.imageLoader.displayImage(
					StringUtils.getUri(appInfo.getPath()), viewHolder.image,
					options);
		}
		viewHolder.mCheckBox.setChecked(appInfo.isCheckFlag());
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				callBack.checkedInfo(appInfo);
			}
		});
		return convertView;
//		try {
//			final ViewHolder viewHolder;
//			if (convertView == null) {
//				convertView = LayoutInflater.from(context).inflate(
//						BaseRProxy.file_img_child_item, null);
//				viewHolder = new ViewHolder(convertView);
//				convertView.setTag(viewHolder);
//			} else {
//				viewHolder = (ViewHolder) convertView.getTag();
//			}
//
//			final ImgGridAdapter adapter = new ImgGridAdapter(context,
//					parentlist.get(groupPosition).getImgList(), this);
//			((Activity) context).runOnUiThread(new Runnable() {
//
//				@Override
//				public void run() {
//					try {
//						System.out
//								.println("-----------------------viewHolder.gridview:"
//										+ viewHolder.gridview);
//						viewHolder.gridview.setAdapter(adapter);// Adapter
//						int totalHeight = (int) FileMg.dip2pixel(context,
//								(adapter.getCount() + 1) / 4.0f * 86.0f);
//						RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) viewHolder.gridview
//								.getLayoutParams(); // 取控件mGrid当前的布局参数
//						linearParams.height = totalHeight;// 当控件的高强制设成75象素
//						viewHolder.gridview.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件mGrid2
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			});
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return convertView;
	}
	class ViewHolder {
		public TextView title;
		public ImageView image;
		public CheckBox mCheckBox;
		public TextView appSize;
		public TextView appTime;
		public ViewHolder(View v){
			title = (TextView) v.findViewById(BaseRProxy.ItemText);
			mCheckBox = (CheckBox) v.findViewById(BaseRProxy.box);
			image = (ImageView) v.findViewById(BaseRProxy.ItemImage);
			appSize = (TextView) v.findViewById(BaseRProxy.ItemText_size);
			appTime = (TextView) v.findViewById(BaseRProxy.ItemText_time);
		}
	}
//	class ViewHolder {
//		public GridView gridview;
//
//		public ViewHolder(View v) {
//			gridview = (GridView) v.findViewById(BaseRProxy.gridview);
//		}
//	}

	@Override
	public int getChildrenCount(int arg0) {
		if(parentlist.get(arg0)==null){
			return 0;
		}
		return parentlist.get(arg0).getImgList().size();
	}

	@Override
	public Object getGroup(int arg0) {
		return parentlist.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return parentlist.size();
	}

	@Override
	public long getGroupId(int arg0) {
		return arg0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolderP viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					BaseRProxy.manager_favoritor_web_item, null);
			viewHolder = new ViewHolderP(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolderP) convertView.getTag();
		}
		ImgPackInfo appInfo = parentlist.get(groupPosition);
		viewHolder.tv_collect.setVisibility(View.GONE);
		viewHolder.tv_add.setVisibility(View.GONE);
		viewHolder.tv_more.setVisibility(View.GONE);
		viewHolder.tv_title.setText(appInfo.getPackName());
		if(isExpanded){
			viewHolder.img.setImageResource(BaseRProxy.expander_ic_minimized);
		}
		else{
			viewHolder.img.setImageResource(BaseRProxy.expander_ic_maximized);
		}
		return convertView;
	}

	class ViewHolderP {
		public TextView tv_collect;
		public ImageView img;
		public TextView tv_more;
		public TextView tv_add;
		public TextView tv_title;

		public ViewHolderP(View arg2) {
			img = (ImageView) arg2
					.findViewById(BaseRProxy.manager_favorite_title_ico);
			tv_collect = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_collection_item);
			tv_add = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_new_folder);
			tv_title = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_title_name);
			tv_more = (TextView) arg2
					.findViewById(BaseRProxy.manager_favorite_more);
		}
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return false;
	}

	@Override
	public void checkedInfo(FileInfo info) {
		// TODO Auto-generated method stub
		this.callBack.checkedInfo(info);
	}
}
